<template>
  <div class="seller-dashboard">
    <!-- 欢迎区域 -->
    <div class="welcome-section">
      <h2>欢迎回来，{{ userStore.userName }}！</h2>
      <p>今天是 {{ currentDate }}，祝您生意兴隆！</p>
    </div>

    <!-- 快捷操作 -->
    <div class="quick-actions">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card class="quick-card" @click="goToProducts">
            <div class="quick-icon">
              <el-icon><Goods /></el-icon>
            </div>
            <div class="quick-text">发布商品</div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="quick-card" @click="goToOrders">
            <div class="quick-icon">
              <el-icon><List /></el-icon>
            </div>
            <div class="quick-text">查看订单</div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 简单数据概览 -->
    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-header">
              <span class="stat-title">今日订单</span>
              <el-icon class="stat-icon"><List /></el-icon>
            </div>
            <div class="stat-value">{{ stats.todayOrders }}</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-header">
              <span class="stat-title">今日收入</span>
              <el-icon class="stat-icon"><Money /></el-icon>
            </div>
            <div class="stat-value">¥{{ stats.todayRevenue }}</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-header">
              <span class="stat-title">商品总数</span>
              <el-icon class="stat-icon"><Goods /></el-icon>
            </div>
            <div class="stat-value">{{ stats.totalProducts }}</div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-header">
              <span class="stat-title">待发货</span>
              <el-icon class="stat-icon"><Bell /></el-icon>
            </div>
            <div class="stat-value">{{ stats.pendingShipment }}</div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { Goods, List, Money, House, Bell } from '@element-plus/icons-vue'
import request from '@/utils/request'

const router = useRouter()
const userStore = useUserStore()

// 当前日期
const currentDate = ref(new Date().toLocaleDateString('zh-CN'))

// 统计数据
const stats = ref({
  todayOrders: 0,
  todayRevenue: 0,
  totalProducts: 0,
  pendingShipment: 0
})

// 页面跳转
const goToProducts = () => router.push('/seller/products')
const goToOrders = () => router.push('/seller/orders')

// 获取统计数据
const fetchStats = async () => {
  try {
    const res = await request.get('/api/seller/stats')
    if (res.success) {
      stats.value = {
        todayOrders: res.data.totalOrders,
        todayRevenue: res.data.totalRevenue,
        totalProducts: res.data.totalProducts,
        pendingShipment: res.data.pendingOrders
      }
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

onMounted(() => {
  fetchStats()
})
</script>

<style scoped>
.seller-dashboard {
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px;
  border-radius: 12px;
  margin-bottom: 30px;
  text-align: center;
}

.welcome-section h2 {
  margin: 0 0 10px 0;
  font-size: 28px;
}

.welcome-section p {
  margin: 0;
  font-size: 16px;
  opacity: 0.9;
}

.quick-actions {
  margin-bottom: 30px;
}

.quick-card {
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  border: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.quick-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}

.quick-icon {
  font-size: 40px;
  color: #409EFF;
  margin-bottom: 10px;
}

.quick-text {
  font-size: 16px;
  color: #606266;
  font-weight: 500;
}

.stats-section {
  margin-bottom: 30px;
}

.stat-card {
  border: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.stat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.stat-title {
  font-size: 14px;
  color: #909399;
}

.stat-icon {
  font-size: 20px;
  color: #409EFF;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

@media (max-width: 768px) {
  .welcome-section {
    padding: 20px;
  }
  
  .welcome-section h2 {
    font-size: 24px;
  }
  
  .quick-actions .el-col {
    margin-bottom: 15px;
  }
  
  .stats-section .el-col {
    margin-bottom: 15px;
  }
}
</style>
